/*replication file: Assumes Stata v14 and "vioplot" and "outreg2" installed*/
clear
set more off
*cd "\whatever\whatever"  /*file path requires specification */
cd E:\Dropbox\Data\CCEScum
use cces_common_cumulative_4.dta /*directly dl'd from https://goo.gl/AYCMpT */
keep if year==2008 | year==2012 /*only 08 and 12 */

/*Merge Zipcode based pop density data to observations*/
#delimit ;
drop if zip_pre=="R0M0J" | zip_pre=="t0b3p" | zip_pre=="8503`" | zip_pre==".";
destring zip_pre, replace;
sort zip_pre ;
merge m:m zip_pre  using zipdensity.dta , gen(zipdensitymergecode);
rename  Density_Per_Sq_Mile zipdens;
drop if zipdens==0;
replace zipdens=1 if zipdens<1; /*very sparse population boosted to 1 person/sqmi*/
gen lgdens = ln(zipdens);

/*merge zipcode-level demographic data */
gen zipcode = zip_pre ; 
sort zipcode ;
merge m:m zipcode using zipdemographicdata.dta, gen(zipdemographicmergecode);

/*basic recodes for ease of coding later*/
gen dempid = .;
recode dempid .=1 if pid3==1;
recode dempid .=0 if pid3==2 | pid3==3;
gen goppid = .;
recode goppid .=1 if pid3==2;
recode goppid .=0 if pid3==1 | pid3==3;
gen conserv = .;
recode conserv .=1 if ideo5==4 | ideo5==5;
recode conserv .=0 if ideo5<=3 | ideo5==6;
gen romneyvoter2012 = .;
recode romneyvoter2012 .=0 if vote_pres_12==1;
recode romneyvoter2012 .=1 if vote_pres_12==2;
gen obamavoter2012 = romneyvoter2012;
recode obamavoter2012 0=1 1=0;
gen mccainvoter2008 = .;
recode mccainvoter2008 .=0 if vote_pres_08==1;
recode mccainvoter2008 .=1 if vote_pres_08==2;
gen obamavoter2008 = mccainvoter2008;
recode obamavoter2008 0=1 1=0 ;
recode family_income 97=.;
recode family_income_old 15=.;
gen hundredk=0;
recode hundredk 0=1 if family_income>9 & family_income<=32;
recode hundredk 0=1 if family_income_old>11 & family_income_old<=14;
generate ba = 0;
recode ba 0=1 if educ==5 | educ==6;
keep if age>19 & age<81;
gen bage = .;
recode bage .=2 if age>19 & age<30;
recode bage .=3 if age>29 & age<40;
recode bage .=4 if age>39 & age<50;
recode bage .=5 if age>49 & age<60;
recode bage .=6 if age>59 & age<70;
recode bage .=7 if age>69 & age<80;
gen marrspouse = 0;
recode marrspouse 0=1 if marriage_status==1;
gen latino = 0;
recode latino 0=1 if race==3;
gen black = 0;
recode black 0=1 if race==2;
gen whitenonhisp = 0;
recode whitenonhisp 0=1 if race==1;
gen twowayrace = . ;
recode twowayrace .=1 if whitenonhisp==1;
recode twowayrace .=2 if black==1;
label define twoway 1 "White" 2 "Black";
label values twoway twoway;

/*Create Region Controls à la Black and Black*/
gen region = .;
recode region .=1 if state_pre==9 | state_pre==10 | state_pre==11 | state_pre==23 |
	state_pre==24 | state_pre==25 | state_pre==33 | state_pre==34 | state_pre==36
	| state_pre==42 | state_pre==44 | state_pre==50 ; /*NE*/
recode region .=2 if state_pre==1 | state_pre==5 | state_pre==12 | state_pre==13
	| state_pre==22 | state_pre==28 | state_pre==37 | state_pre==46 | state_pre==47
	| state_pre==48 | state_pre==51; /*SOUTH*/
recode region .=3 if state_pre==17 | state_pre==18 | state_pre==19 | state_pre==21
	| state_pre==26 | state_pre==27 | state_pre==29 | state_pre==39 | state_pre==54
	| state_pre==55; /*MW*/
recode region .=4 if state_pre==4 | state_pre==8 | state_pre==16 | state_pre==20
	| state_pre==30 | state_pre==31 | state_pre==32 | state_pre==35
	| state_pre==38 | state_pre==40 | state_pre==46 | state_pre==49
	| state_pre==56; /*MOUNT/PLAIN*/
recode region .=5 if state_pre==6 | state_pre==41 | state_pre==53; /*PAC*/


/******************** Show how many zipcodes involved **********/
#delimit ;
quietly logit obamavoter2012 goppid dempid conserv black latino gender marrspouse bage ba hundredk region lgdens zipwhiteperc if year==2012;
di "2012"; codebook zip_pre if e(sample);
quietly logit obamavoter2008 goppid dempid conserv black latino gender marrspouse bage ba hundredk region lgdens zipwhiteperc if year==2008;
di "2008"; codebook zip_pre if e(sample);

/*****************Making Table 1*********************************/
/**********************2008**************************************/
logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.black 
		i.latino 
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk 
		i.region
		c.lgdens
		c.zipwhiteperc
		[pweight=weight] if year==2008 ;
		outreg2 using table.xlsx, excel replace ctitle(2008_1) bdec(2) 2aster;
		
logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.black 
		i.latino 
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk 
		i.region
		c.lgdens
		c.zipblackperc
		[pweight=weight] if year==2008 ;
		outreg2 using table.xlsx, excel append ctitle(2008_2) bdec(2) 2aster;
		
logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipwhiteperc
		[pweight=weight] if black==1 & year==2008 ;
		outreg2 using table.xlsx, excel append ctitle(2008_3) bdec(2) 2aster;

logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipwhiteperc
		[pweight=weight] if white==1 & year==2008 ;
		outreg2 using table.xlsx, excel append ctitle(2008_4) bdec(2) 2aster;

logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipblackperc
		[pweight=weight] if white==1 & year==2008 ;
		outreg2 using table.xlsx, excel append ctitle(2008_5) bdec(2) 2aster;
/*********************2012**************************************/

#delimit ;
set more off;

logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.black 
		i.latino 
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk 
		i.region
		c.lgdens
		c.zipwhiteperc
		[pweight=weight] if year==2012 ;
		outreg2 using table.xlsx, excel append ctitle(2012_1) bdec(2) 2aster;
logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.black 
		i.latino 
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk 
		i.region
		c.lgdens
		c.zipblackperc
		[pweight=weight] if year==2012 ;
		outreg2 using table.xlsx, excel append ctitle(2012_2) bdec(2) 2aster;
		
logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipwhiteperc
		[pweight=weight] if black==1 & year==2012 ;
		outreg2 using table.xlsx, excel append ctitle(2012_3) bdec(2) 2aster;

logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipwhiteperc
		[pweight=weight] if white==1 & year==2012 ;
		outreg2 using table.xlsx, excel append ctitle(2012_4) bdec(2) 2aster;

logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipblackperc
		[pweight=weight] if white==1 & year==2012 ;
		outreg2 using table.xlsx, excel append ctitle(2012_5) bdec(2) 2aster;
/*******END OF MAKING TABLE 1**************/

/*******Reveiwer 1 wondered if there were a South/Nonsouth dynamic, so... ******/
/*******************************************************************************/
#delimit ;
gen southdummy = 0;
recode southdummy 0=1 if region==2;
#delimit ;

quietly logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		c.lgdens##c.zipblackperc##i.southdummy
		[pweight=weight] if white==1 & year==2008 ;
		outreg2 using southnonsouth.xls, excel replace ctitle(2008) bdec(2) 2aster;
quietly logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		c.lgdens##c.zipblackperc##i.southdummy
		[pweight=weight] if white==1 & year==2012 ;
		outreg2 using southnonsouth.xls, excel append ctitle(2012) bdec(2) 2aster;
	
/*Make figure4*/
/************2008************************/
#delimit ;
set more off;
logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		c.lgdens##c.zipblackperc##i.southdummy
		[pweight=weight] if white==1 & year==2008 ;
		margins , at(  zipblackperc=(0(10)100) lgdens=(8.1 5.2) southdummy=(0) );
	#delimit ;
		marginsplot ,
			scheme(s1mono)
			title(" ")
			plot1opts(msymbol(none))
			plot2opts(msymbol(none))
			legend(
				position(7)
				ring(0)
				row(1)
				order(1 "Low Density" 2 "High Density")
				)
			yscale(range(.2(.1).6))
			ylabel(.2(.1).6, angle(horizontal))
			ytitle(" ")
			noci
			xtitle(" ")
			title(4a. 2008 Non-South White Voters)
			;
		graph save Graph "Fig4nonsouth2008.gph", replace;
	#delimit ;
		margins , at(  zipblackperc=(0(10)100) lgdens=(8.1 5.2) southdummy=(1) );
		#delimit ;
		marginsplot ,
			scheme(s1mono)
			title(" ")
			plot1opts(msymbol(none))
			plot2opts(msymbol(none))
			legend(
				position(7)
				ring(0)
				row(1)
				order(1 "Low Density" 2 "High Density")
				)
		yscale(range(.2(.1).6))
		ylabel(.2(.1).6, angle(horizontal))
		ytitle(" ")
		noci
		xtitle(" ")
		title(4b. 2008 South White Voters)
		;
		graph save Graph "Fig4south2008.gph", replace;
/************2012************************/
#delimit ;
logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		c.lgdens##c.zipblackperc##i.southdummy
		[pweight=weight] if white==1 & year==2012 ;
		margins , at(  zipblackperc=(0(10)100) lgdens=(8.1 5.2) southdummy=(0) );
	#delimit ;
		marginsplot ,
			scheme(s1mono)
			title(" ")
			plot1opts(msymbol(none))
			plot2opts(msymbol(none))
			legend(
				position(7)
				ring(0)
				row(1)
				order(1 "Low Density" 2 "High Density")
				)
			yscale(range(.2(.1).6))
			ylabel(.2(.1).6, angle(horizontal))
			ytitle(" ")
			noci
			xtitle(" ")
			title(4c. 2012 Non-South White Voters)
			;
		graph save Graph "Fig4nonsouth2012.gph", replace;
	#delimit ;
		margins , at(  zipblackperc=(0(10)100) lgdens=(8.1 5.2) southdummy=(1) );
	#delimit ;
		marginsplot ,
			scheme(s1mono)
			title(" ")
			plot1opts(msymbol(none))
			plot2opts(msymbol(none))
			legend(
				position(7)
				ring(0)
				row(1)
				order(1 "Low Density" 2 "High Density")
				)
		yscale(range(.2(.1).6))
		ylabel(.2(.1).6, angle(horizontal))
		ytitle(" ")
		noci
		xtitle(" ")
		title(4d. 2012 South White Voters)
		;
		graph save Graph "Fig4south2012.gph", replace;
#delimit ;
		graph combine Fig4nonsouth2008.gph Fig4south2008.gph Fig4nonsouth2012.gph Fig4south2012.gph,
	ycommon
	xcommon
	scheme(s1mono)
	scale(.85)
	l1title("Likelihood Obama Voter")
	b1title("Percent Black in R's ZC")
	;
graph save Graph "Fig4.gph", replace;
graph export "Fig4.png", as(png) replace; 
 



		
		

/***************Making Figures ******************************/
/*Making figure 1a-1b: distribution of race distrib in zipcodes per R group*/
/************************************************************/
#delimit ;
drop inmodels;
#delimit ;
quietly logit obamavoter2012 goppid dempid conserv black latino gender marrspouse bage ba hundredk region lgdens zipwhiteperc if year==2012;
gen inmodels=1 if e(sample);
quietly logit obamavoter2008 goppid dempid conserv black latino gender marrspouse bage ba hundredk region lgdens zipwhiteperc if year==2008;
recode inmodels .=1 if e(sample);
vioplot zipblackperc if inmodels==1 , 
	over(twoway) 
	scheme(s1mono) 
	title(1b. Black% in Zipcode)
	ylabel(, angle(horizontal))
	;
	graph export "blackzip.png", as(png) replace;
	graph save Graph "blackzip.gph", replace;
#delimit ;	
vioplot zipwhiteperc if inmodels==1, 
	over(twoway) 
	scheme(s1mono) 
	title(1a. White% in Zipcode)
	ylabel(, angle(horizontal))
	;
	graph export "whitezip.png", as(png) replace;
	graph save Graph "whitezip.gph", replace;
graph combine whitezip.gph blackzip.gph, 
	scheme(s1mono) row(1) ycommon b1(Respondent's race);	
/********end of figure 1a-1b**********common for 2008 and 2012***/



/*************Figure 2a, 2b, 2c, 2d for 2008 & 2012**************************/
#delimit ;
set more off;
logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipwhiteperc
		[pweight=weight] if white==1 & year==2012 ;
margins , at( lgdens=(1(2)12)  zipwhiteperc=(20 95));
#delimit ;
marginsplot , 
		scheme(s1mono) 
		title(2d. White Voters 2012) 
		legend(off)
		yscale(range(.2(.2)1)) 
		ylabel(.2(.2)1, angle(horizontal))
		noci
		xtitle(" ")
		ytitle(" ")
		;
graph save Graph "whites_12.gph", replace;

#delimit ;
logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipwhiteperc
		[pweight=weight] if black==1 & year==2012 ;
margins , at( lgdens=(1(2)12)  zipwhiteperc=(20 95));
#delimit ;
marginsplot , 
		scheme(s1mono) 
		title(2b. Black Voters 2012) 
		legend(
			off
			) 
		yscale(range(.2(.2)1)) 
		ylabel(.2(.2)1, angle(horizontal))
		noci
		ytitle(" ")
		xtitle(" ")
		;
graph save Graph "blacks_12.gph", replace;

#delimit ;
set more off;
logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipwhiteperc
		[pweight=weight] if white==1 & year==2008 ;
margins , at( lgdens=(1(2)12)  zipwhiteperc=(20 95));
#delimit ;
marginsplot , 
		scheme(s1mono) 
		title(2c. White Voters 2008) 
		legend(off)
		yscale(range(.2(.2)1)) 
		ylabel(.2(.2)1, angle(horizontal))
		noci
		xtitle(" ")
		ytitle(" ")
		;
graph save Graph "whites_08.gph", replace;

#delimit ;
logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipwhiteperc
		[pweight=weight] if black==1 & year==2008 ;
margins , at( lgdens=(1(2)12)  zipwhiteperc=(20 95));
#delimit ;
marginsplot , 
		scheme(s1mono) 
		title(2a. Black Voters 2008) 
		legend(
			col(1) 
			pos(6) 
			ring(0)
			order(1 "20% White Zip" 2 "95% White Zip")
			) 
		yscale(range(.2(.2)1)) 
		ylabel(.2(.2)1, angle(horizontal))
		ytitle(" ")
		noci
		xtitle(" ")
		
		;
graph save Graph "blacks_08.gph", replace;

#delimit ;
graph combine  "blacks_08.gph" "blacks_12.gph" "whites_08.gph" "whites_12.gph" 
,
scheme(s1mono)
b1(Logged Population Density)
l1(Prob. Obama Vote)
;
/**********End of Figure 2*****************/	

 
 
/******Figure 3********************/
#delimit ;
set more off ;

logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipblackperc
		[pweight=weight] if year==2012 & white==1;
		#delimit ;

		margins , at(  zipblackperc=(0(10)100) lgdens=(5.2 8.1) );
		#delimit ;
		marginsplot ,
			scheme(s1mono)
			title(" ")
			legend(
				off
				)
		yscale(range(.2(.1).6))
		ylabel(.2(.1).6, angle(horizontal))
		ytitle(" ")
		noci
		xtitle(" ")
		title(3b. 2012 White Voters)
		;
graph save Graph "high_low_contrast_across_black_12.gph", replace;
#delimit ;
set more off ;

logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk
		i.region
		c.lgdens##c.zipblackperc
		[pweight=weight] if year==2008 & white==1;
		#delimit ;

		margins , at(  zipblackperc=(0(10)100) lgdens=(5.2 8.1) );
		#delimit ;
		marginsplot ,
			scheme(s1mono)
			title(" ")
			legend(
				col(1)
				pos(7)
				ring(0)
				order(1 "25th Percentile Density" 2 "75th Percentile Density")
				)
		yscale(range(.2(.1).6))
		ylabel(.2(.1).6, angle(horizontal))
		ytitle(" ")
		noci
		xtitle(" ")
		title(3a. 2008 White Voters)
				
				
				;
graph save Graph "high_low_contrast_across_black_08.gph", replace ;

#delimit ;
graph combine
		"high_low_contrast_across_black_08.gph" 
		"high_low_contrast_across_black_12.gph"
,
scheme(s1mono)
b1(R's Zip Code's Black%)
l1(Prob. of Obama Vote)
;

/*****************Appendix Figure A1a-A1b*******************/
/*Making figure : "Estimated impact of Pop Density on Obama Vote"*/
#delimit ;
set more off;
logit obamavoter2012 
		i.goppid 
		i.dempid
		i.conserv
		i.black 
		i.latino 
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk 
		i.region
		c.lgdens
		c.zipwhiteperc
		[pweight=weight] if year==2012 ;
#delimit ;
margins , at(lgdens=(2 4 6 8 10 12));
marginsplot, 
	ciopts(fcolor(gs12))
	xdimension(at(lgdens),  allsimplelabels noseparator) 
	recastci(rarea) 
	scheme(s1mono) 
	text(.54 2.08  "≈10 / sq mi", orient(vertical))
	text(.55 4  "≈50 / sq mi", orient(vertical))
	text(.56 6  "≈400/ sq mi", orient(vertical))
	text(.58 8  "≈3k / sq mi", orient(vertical))
	text(.52 10  "≈20k / sq mi", orient(vertical))
	text(.53 11.8 "≈160k / sq mi", orient(vertical))
	xtitle(" ")
	ytitle("Prob. Vote for Obama 2012")
	xline(6.74)
	title("A2. Model 6 in 2012")
	yscale(range(.45 .6))
	ylabel(.45(.05).6, angle(horizontal) format(%3.2f))
;
graph save Graph "app2012.gph", replace ;
#delimit ;
logit obamavoter2008 
		i.goppid 
		i.dempid
		i.conserv
		i.black 
		i.latino 
		i.gender 
		i.marrspouse 
		i.bage 
		i.ba 
		i.hundredk 
		i.region
		c.lgdens
		c.zipwhiteperc
		[pweight=weight] if year==2008 ;
#delimit ;
margins , at(lgdens=(2 4 6 8 10 12));
marginsplot, 
	ciopts(fcolor(gs12))
	xdimension(at(lgdens),  allsimplelabels noseparator) 
	recastci(rarea) 
	scheme(s1mono) 
	text(.545 2.08  "≈10 / sq mi", orient(vertical))
	text(.55 4  "≈50 / sq mi", orient(vertical))
	text(.56 6  "≈400/ sq mi", orient(vertical))
	text(.58 8  "≈3k / sq mi", orient(vertical))
	text(.53 10  "≈20k / sq mi", orient(vertical))
	text(.53 11.8 "≈160k / sq mi", orient(vertical))
	xtitle(" ")
	ytitle("Prob. Vote for Obama 2008")
	xline(6.66) 
	title("A1. Model 1 in 2008")
	yscale(range(.45 .6))
	ylabel(.45(.05).6, angle(horizontal) format(%3.2f))
;
graph save Graph "app2008.gph", replace ;
#delimit ;
graph combine 
	"app2008.gph"
	"app2012.gph"
	,
	rows(1)
	scheme(s1manual) 
	commonscheme
	b1(Logged Pop. Density at Zip Code Level)
;

graph save  "two-paned-overall-effect.gph", replace ;
